const express = require("express");
const router = express.Router();
const passport = require('passport');
const Food = require("../../models/Food");
const pagination = require('mongoose-sex-page')

router.get('/Food/list/all', passport.authenticate("jwt", { session: false }), (req, res) => {
  let foodFields = {}
  if (req.query.foodId) foodFields.foodId= req.query.foodId
  if (req.query.models) foodFields.models = req.query.models
  if (req.query.FoodAddress) foodFields.FoodAddress = req.query.FoodAddress
  if (req.query.packageType) foodFields.packageType = req.query.packageType
  if (req.query.price) foodFields.price = req.query.price
  if (req.query.name) foodFields.name = req.query.name
  if (req.query.number) foodFields.number = req.query.number
  if (req.query.telephone) foodFields.telephone = req.query.telephone
  if (req.query.UserAddress) foodFields.UserAddress = req.query.UserAddress
  if (req.query.createTime) foodFields.createTime = req.query.createTime
  if (req.query.endTime) foodFields.endTime = req.query.endTime
  if (req.query.status) foodFields.status = req.query.status
  // page 当前页
  Food.find(foodFields).sort({ "createTime": -1 })
    .then(Food => {
      if (!Food) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = Food.length;
      return res.json({ status: 200, message: "请求成功", success: true, data: Food, total })
    })
    .catch(error => {
      return res.status(400).json({ status: 400, error, message: "获取列表失败" })
    })
})

router.get('/Food/list', passport.authenticate("jwt", { session: false }), (req, res) => {
  let foodFields = {}
  if (req.body.foodId) foodFields.foodId= req.body.foodId
  if (req.query.models) foodFields.models = req.query.models
  if (req.query.FoodAddress) foodFields.FoodAddress = req.query.FoodAddress
  if (req.query.packageType) foodFields.packageType = req.query.packageType
  if (req.query.price) foodFields.price = req.query.price
  if (req.query.name) foodFields.name = req.query.name
  if (req.query.number) foodFields.number = req.query.number
  if (req.query.telephone) foodFields.telephone = req.query.telephone
  if (req.query.UserAddress) foodFields.UserAddress = req.query.UserAddress
  if (req.query.createTime) foodFields.createTime = req.query.createTime
  if (req.query.endTime) foodFields.endTime = req.query.endTime
  if (req.query.status) foodFields.status = req.query.status
  // page 当前页
  pagination(Food).find(foodFields).sort({ "createTime": -1 }).page(req.query.pageNum).size(req.query.pageSize).exec()
    .then(Food => {
      if (!Food) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = Food.length;
      return res.json({ status: 200, message: "请求成功", success: true, data: Food, total })
    })
    .catch(error => {
      return res.status(400).json({ status: 400, error, message: "获取列表失败" })
    })
})
// jwt的一个插件库
router.post('/Food/add', passport.authenticate('jwt', { session: false }), (req, res) => {
  let foodFields = {}
  if (req.body.foodId) foodFields.foodId= req.body.foodId
  if (req.body.models) foodFields.models = req.body.models
  if (req.body.FoodAddress) foodFields.FoodAddress = req.body.FoodAddress
  if (req.body.packageType) foodFields.packageType = req.body.packageType
  if (req.body.price) foodFields.price = req.body.price
  if (req.body.name) foodFields.name = req.body.name
  if (req.body.number) foodFields.number = req.body.number
  if (req.body.telephone) foodFields.telephone = req.body.telephone
  if (req.body.UserAddress) foodFields.UserAddress = req.body.UserAddress
  if (req.body.createTime) foodFields.createTime = req.body.createTime
  if (req.body.endTime) foodFields.endTime = req.body.endTime
  if (req.body.status) foodFields.status = req.body.status;
  console.log(foodFields,'foodFields');
  new Food(foodFields).save()
    .then(() => {
      return res.json({ status: 200,foodFields, success: true, message:foodFields })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: error })
    });
});

router.post('/Food/edit/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
   let foodFields = {}
  if (req.body.foodId) foodFields.foodId= req.body.foodId
  if (req.body.models) foodFields.models = req.body.models
  if (req.body.FoodAddress) foodFields.FoodAddress = req.body.FoodAddress
  if (req.body.packageType) foodFields.packageType = req.body.packageType
  if (req.body.price) foodFields.price = req.body.price
  if (req.body.name) foodFields.name = req.body.name
  if (req.body.number) foodFields.number = req.body.number
  if (req.body.telephone) foodFields.telephone = req.body.telephone
  if (req.body.UserAddress) foodFields.UserAddress = req.body.UserAddress
  if (req.body.createTime) foodFields.createTime = req.body.createTime
  if (req.body.endTime) foodFields.endTime = req.body.endTime
  if (req.body.opTime) foodFields.endTime = req.body.opTime
  if (req.body.status) foodFields.status = req.body.status;
  Food.findOneAndUpdate(
    { _id: req.params.id },
    { $set: foodFields  },
    { new: true })
    .then(() => {
      return res.json({ status: 200, success: true, message: '修改成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '修改失败!' })
    });
});



router.delete('/Food/delete/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  Food.findOneAndDelete({ _id: req.params.id })
    .then(Food => {
      return res.json({ status: 200, success: true, message: '删除成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '删除失败!' })
    });
});


router.get('/Food/detail/:id', passport.authenticate("jwt", { session: false }), (req, res) => {
  Food.findOne({ _id: req.params.id })
    .then(Food => {
      return res.json({ status: 200, message: "请求成功", success: true, data: Food })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, message: " 获取信息失败", error })
    })
})

module.exports = router
